package com.yunxin.customui.bean.table;

import com.alibaba.fastjson.annotation.JSONField;
import com.yunxin.tools.commonbean.DataBean;
import com.yunxin.tools.commonenum.SpecialValueEnum;
import com.yunxin.customui.bean.table.datatable.TableDataTemplate;
import com.yunxin.customui.myinterface.table.FormulaData;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.text.DecimalFormat;

/**
 * 功能描述:
 *     前台表格展示单元格Bean
 * date 2021/8/2 14:20
 *
 * @author cc
 * @version 1.0
 */

@AllArgsConstructor
@NoArgsConstructor
public class TdBean implements Serializable , FormulaData {

    @Getter@Setter
    protected String fieldName;

    /**
     * 单元格中展示的值
     */
    @Getter@Setter
    protected String value;

    @Getter
    @JSONField(serialize = false)
    protected String time;

    @Getter
    protected String id;

    @Getter@Setter
    protected TableStyle style = new TableStyle();
    @JSONField(serialize = false)

    @Getter
    protected String calHandle;
    /**
     * html处理，默认，将-9999转为/
     */
    @JSONField(serialize = false)
    @Getter@Setter
    protected String htmlHandle = "changeText(=,-9999,/);";

    /**
     * 列号
     */
    @Getter@Setter
    protected int col = -1;
    /**
     * 行号
     */
    @Getter@Setter
    protected int row = -1;

    // 合计显示的时候要合并几个单元格
    @Getter@Setter
    Integer rowspan;

    @Getter@Setter
    Integer colspan;

    //格式化value，保留两位， 暂时这么做， 之后应该会变成htmlHandler的一个策略
    private final DecimalFormat decimalFormat = new DecimalFormat("0.00");

    public TdBean(String value) {
        this.value = value;
    }

    /**
     * 单元格填入数据
     *
     * @param tableDataTemplate 列配置
     * @param dataBean           数据bean
     */
    public void setData(TableDataTemplate tableDataTemplate, DataBean dataBean){
        if (dataBean != null) {
            this.id=dataBean.getDataId();
            this.time = dataBean.getDataTime();
            try {
                this.value= decimalFormat.format(dataBean.getDataValue());
            }catch (NumberFormatException e){
                System.out.println(value);
                this.value= String.valueOf(dataBean.getDataValue());
            }

            if (tableDataTemplate.getHtmlHandle()!=null){
                this.htmlHandle+= tableDataTemplate.getHtmlHandle();
            }
            this.calHandle= tableDataTemplate.getCalHandle();
        } else {
            this.value=String.valueOf(SpecialValueEnum.invalid.getValue());
        }
    }

    /**
     * 单元格填入数据
     *
     * @param tableDataTemplate 列配置
     * @param value              值
     */
    public void setData(TableDataTemplate tableDataTemplate, String value){
        try {
            this.value= decimalFormat.format(Double.parseDouble(value));
        }catch (NumberFormatException e){
            this.value= String.valueOf(value);
        }
            if (tableDataTemplate.getHtmlHandle()!=null){
                this.htmlHandle+= tableDataTemplate.getHtmlHandle();
            }
            this.calHandle= tableDataTemplate.getCalHandle();
    }

    @Override
    public String formulaData() {
        return this.value;
    }
}
